extends Area2D

# 褰撶帺瀹惰繘鍏ュ尯鍩熸椂瑙﹀彂
func _on_body_entered(body):
    if body.name == "Player":
        # 清理 level1 场景中的资源和节点
        # clear_nodes()

        # 确保 "Level2" 场景的资源路径是正确的
        if SceneRegistry.levels.has("Level2"):
            var level_2_scene_path = SceneRegistry.levels["Level2"]

            # 尝试获取根节点下的 "game_start" 节点
            # var game_start_node = get_tree().root.get_node("Level01")
            # if game_start_node == null:
            #     print("Cannot find 'game_start' node in the root of the scene tree.")
            #     return
            var oldscene = get_tree().get_root().get_node("Level01")
            var old_children = oldscene.get_children()
            for child in old_children:
                child.queue_free()
            # 切换到 level2 场景
            # get_tree().current_scene = get_tree().get_root().get_node("Level02")
            SceneManager.swap_scenes(level_2_scene_path, get_tree().root, oldscene, "wipe_to_right")
            # get_tree().current_scene = get_tree().get_root().get_node("Level02")
            # get_tree().reload_current_scene()
        else:
            print("Scene 'Level2' is not registered in SceneRegistry.")

# 娓呯悊鑺傜偣鍜岃祫婧�
func clear_nodes():

    var new_scene = ResourceLoader.load(SceneRegistry.levels["Level2"])
    if new_scene is PackedScene:
        # get_tree().current_scene = new_scene.instance()
        for child in new_scene.instance().get_children():
            child.queue_free()
